package org.mtdownload.listview;

import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;

import org.mtdownload.app.Icons;
import org.mtdownload.download.DownloadManager;
import org.mtdownload.download.DownloadTask;
import org.mtdownload.download.DownloadTaskPropertiesChanged;

public class DownloadsListTableModel extends DefaultTableModel {

	private static final long serialVersionUID = 3257571698076235057L;
	//keep our own vector of tasks
	private Vector<DownloadTask> tasks=new Vector<DownloadTask>();
	
	private ImageIcon downloading=null;
	private ImageIcon downloadingError=null;
	private ImageIcon downloadingComplete=null;
	
	public DownloadsListTableModel() {
		super(DownloadTask.columnNames,0);
		DownloadManager.addDownloadTaskPropertiesChangedListener(new DownloadTaskPropertiesChanged(){
			public void propertiesChanged(DownloadTask task) {
				int index=tasks.indexOf(task);
				if(index<0||index>=tasks.size()){
					addDownloadTask(task);
				}else{
					updateTask(task);
				}
			}
		});
		downloading=new ImageIcon(DownloadsListTableModel.class.getResource(Icons.DOWNLOADING));
		downloadingError=new ImageIcon(DownloadsListTableModel.class.getResource(Icons.DOWNLOADING_ERROR));
		downloadingComplete=new ImageIcon(DownloadsListTableModel.class.getResource(Icons.DOWNLOADING_COMPLETED));
	}
	
	public void updateTask(DownloadTask task){
		int index=tasks.indexOf(task);
		if(index<0||index>=tasks.size()){
			return;
		}
		if(task.isStarted()){
			setValueAt(downloading,index,0);
		}else{
			if(task.isComplete()){
				setValueAt(downloadingComplete,index,0);
			}else{
				setValueAt(downloadingError,index,0);
			}
		}
		setValueAt(task.getDestinationFileName(),index,1);
		setValueAt(task.getStatus(),index,2);
		setValueAt(task.getHumanFriendlyPercent(),index,3);
		setValueAt(task.getHumanFriendlySpeed(DownloadTask.SPEED_KILOBYTES) + " KB/s",index,4);
		setValueAt(task.getHumanFriendlyBytes(task.getCompletedBytes())+"/"+task.getHumanFriendlyBytes(task.getFileLength()),index,5);
		setValueAt(task.getHumanFriendlyTime(task.getEta()),index,6);
		setValueAt(task.getHumanFriendlyTime(task.getElapsed()),index,7);
		setValueAt( task.getThreadNum() + "",index,8);
		setValueAt(task.getUrl(),index,9);
		
		fireTableRowsUpdated(index,index);
	}
	
	public void removeDownloadTask(DownloadTask task){
		int index=tasks.indexOf(task);
		if(index>=0&&index<tasks.size()){
			tasks.remove(task);			
			removeRow(index);
		}
	}
	
	public void addDownloadTask(DownloadTask task){
		Vector<Object> data=new Vector<Object>();		
		data.add(downloadingError);		
		data.add(task.getDestinationFileName());
		data.add(task.getStatus());
		data.add(task.getHumanFriendlyPercent());
		data.add(task.getHumanFriendlySpeed(DownloadTask.SPEED_KILOBYTES) + " KB/s");
		data.add(task.getHumanFriendlyBytes(task.getCompletedBytes())+"/"+task.getHumanFriendlyBytes(task.getFileLength()));
		data.add(task.getHumanFriendlyTime(task.getEta()));
		data.add(task.getHumanFriendlyTime(task.getElapsed()));
		data.add( task.getThreadNum() + "");
		data.add(task.getUrl());
		addRow(data);
		tasks.add(task);
	}
	
    public Class<?> getColumnClass(int c) {
		if(c==0){
			return ImageIcon.class;
		}
        return String.class;
    }

	/* (non-Javadoc)
	 * @see javax.swing.table.DefaultTableModel#isCellEditable(int, int)
	 */
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.DefaultTableModel#getColumnCount()
	 */
	public int getColumnCount() {
		return DownloadTask.columnNames.length;
	}

}
